<template>
  <div class="skill-wrap">
    <div class="chose-course-top">
      <div class="header-left">
        <span class="back-arrow" @click="closeHandle"></span>
      </div>
      <div class="course-title">
        考点解读
      </div>
      <div class="btn-group">
        <a :disabled="printDisabled" @click="print">打印</a>
      </div>
    </div>
    <div class="skill-content" ref="contentToExport">
      <div class="skill-info" v-html="info"></div>
    </div>
  </div>
</template>
<script>
// import _ from "lodash";
import {grammarDetail} from "@/api/reverse";
import {mapState} from "vuex";
import html2pdf from "html2pdf.js";

export default {
  components: {},
  data() {
    return {
      info: {},
      printDisabled: false,
    };
  },
  computed: {
    ...mapState({
      reverseStatus: (state) => state.reverseStatus,
      // grammarExplainVisible: (state) => state.grammarExplainVisible,
    }),
  },
  watch: {
    grammarExplainVisible: function(val) {
      // if (val === true) {
      //   grammarPoint({
      //     ..._.pick(this.reverseStatus, ["chapters_id"]),
      //   }).then((res) => {
      //     if (res.code === SUCCESS_CODE) {
      //       this.info = res.data;
      //     }
      //   });
      // }
    },
  },
  methods: {
    // ...mapMutations(["setGrammarExplain"]),
    closeHandle() {
      switch (this.$route.query.title){
        case 'sentence':
          this.$router.push({
            path: '/reverse/grammar-sentence',
            query: {
              userId:this.reverseStatus.userId
            },
          })
              break
        case 'into':
          this.$router.push({
            path: '/reverse/scene-into',
            query: {
              userId:this.reverseStatus.userId
            },
          })
              break
        default:
            this.$router.push({
              path: '/study/grammar?ref=homepage',
              query: {
                userId:this.reverseStatus.userId
              },
            })
      }
      // if (this.$route.query.title ===){
      //   this.$router.push({
      //     path: '/reverse/grammar-sentence'
      //   })
      // }else  {
      //   this.$router.push({
      //     path: '/study/grammar?ref=homepage'
      //   })
      // }

      // this.setGrammarExplain(false);
    },
    print() {
      const element = this.$refs.contentToExport;
      const options = {
        margin:       1,
        filename:     '考前解读.pdf',
        image:        { type: 'jpeg', quality: 0.98 },
        html2canvas:  { dpi: 192, letterRendering: true },
        jsPDF:        { unit: 'mm', format: 'a4', orientation: 'portrait' }
      };
      html2pdf()
          .from(element)
          .set(options)
          .save();
    },
    printInterval() {
      //倒计时
      let time = 60;
      this.printDisabled = true;
      let timer = setInterval(() => {
        if (time == 0) {
          clearInterval(timer);
          this.printDisabled = false;
        } else {
          this.printDisabled = true;
          time--;
        }
      }, 1000);
    },
  },
  async created() {
    let id = this.$route.query.id
    await grammarDetail({id:id}).then((res)=>{
      this.info = res.data;
    })
  },
  mounted() {},
  beforeCreate() {},
  beforeMount() {},
  beforeUpdate() {},
  updated() {},
  beforeDestroy() {},
  destroyed() {},
  activated() {},
};
</script>
<style  scoped>
/* @import url(); 引入公共css类 */
.skill-wrap {
  position: fixed;
  z-index: 1999;
  background-color: #fff;
  width: 100%;
  top: 0;
  left: 0;
  display: flex;
  flex-direction: column;
  height: 100vh;
  .chose-course-top {
    display: flex;
    justify-content: space-between;
    position: relative;
    align-items: center;
    height: 80px;
    padding: 0 40px;
    background-color: #fff;
    .anticon {
      font-size: 24px; /* no */
    }
    .header-left {
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: flex-start;
      .back-arrow {
        width: 30px;
        height: 27px;
        background-image: url("@/assets/home/btn_back.png");
        background-size: 100%;
        cursor: pointer;
      }
    }

    .course-title {
      width: 100%;
      flex: 1;
      font-size: 20px;
      font-weight: 700;
      text-align: center;

      .check-title {
        color: #c7c7c7;
        padding: 0 15px;
      }
      .check__active {
        color: #19C14B;
        position: relative;
        &::after {
          content: "";
          position: absolute;
          width: 23px;
          height: 4px;
          background-color: #19C14B;
          bottom: -8px;
          left: 50%;
          transform: translateX(-50%);
        }
      }
    }
    .btn-group {
      flex: 1;
      display: flex;
      font-size: 20px;
      justify-content: flex-end;
      align-items: center;
      & > a {
        display: inline-block;
        color: #19C14B;

        margin-left: 50px;
      }
      .play-text {
        font-weight: 700;
      }
    }
  }
  .skill-content {
    background-image: url("@/assets/home/bg2.png");
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center center;
    .skill-info {
      width: 800px;
      height: calc(100vh - 170px);
      text-align: left;
      overflow-y: auto;
      background-color: #fff;
      margin: 30px auto 60px;
      padding: 40px 80px;
      border-radius: 10px; /* no */
    }
  }
}
</style>
